package com.flower.web.controller.business;

import cn.hutool.core.lang.Snowflake;
import com.flower.common.annotation.Log;
import com.flower.common.core.controller.BaseController;
import com.flower.common.core.domain.AjaxResult;
import com.flower.common.core.page.TableDataInfo;
import com.flower.common.enums.BusinessType;
import com.flower.common.utils.poi.ExcelUtil;
import com.flower.system.domain.BizForum;
import com.flower.system.domain.BizForumReply;
import com.flower.system.service.IBizForumReplyService;
import com.flower.system.service.IBizForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 社区论坛Controller
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/system/forum")
public class BizForumController extends BaseController
{
    @Autowired
    private IBizForumService bizForumService;

    @Autowired
    private IBizForumReplyService bizForumReplyService;


    /**
     * 查询社区论坛列表
     */
    @PreAuthorize("@ss.hasPermi('system:forum:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizForum bizForum)
    {
        startPage();
        List<BizForum> list = bizForumService.selectBizForumList(bizForum);
        return getDataTable(list);
    }

    /**
     * 导出社区论坛列表
     */
    @PreAuthorize("@ss.hasPermi('system:forum:export')")
    @Log(title = "社区论坛", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizForum bizForum)
    {
        List<BizForum> list = bizForumService.selectBizForumList(bizForum);
        ExcelUtil<BizForum> util = new ExcelUtil<BizForum>(BizForum.class);
        util.exportExcel(response, list, "社区论坛数据");
    }

    /**
     * 获取社区论坛详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:forum:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bizForumService.selectBizForumById(id));
    }

    /**
     * 新增社区论坛
     */
    @PreAuthorize("@ss.hasPermi('system:forum:add')")
    @Log(title = "社区论坛", businessType = BusinessType.INSERT)
    @PostMapping({"","/add"})
    public AjaxResult add(@RequestBody BizForum bizForum)
    {
        //添加雪花id
        bizForum.setId(new Snowflake(1,1).nextIdStr());
        //根据token获取登录用户
        bizForum.setUserId(getLoginUser().getUser().getUserId().toString());
        return toAjax(bizForumService.insertBizForum(bizForum));
    }

    /**
     * 修改社区论坛
     */
    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @Log(title = "社区论坛", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizForum bizForum)
    {
        return toAjax(bizForumService.updateBizForum(bizForum));
    }

    /**
     * 删除社区论坛
     */
    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @Log(title = "社区论坛", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizForumService.deleteBizForumByIds(ids));
    }

    /**
     *
     * 根据id查询出帖子并且附上评论
     */
    @PreAuthorize("@ss.hasPermi('system:forum:list')")
    @GetMapping("/getForumsWithReply")
    public AjaxResult getForumsWithReply(String id)
    {
        //并且附上评论
        BizForum bizForum = bizForumService.selectBizForumById(id);
        BizForumReply bizForumReply = new BizForumReply();
        bizForumReply.setForumId(id);
        List<BizForumReply> bizForumReplies = bizForumReplyService.selectBizForumReplyList(bizForumReply);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("forum",bizForum);
        hashMap.put("replyList",bizForumReplies);

        return success(hashMap);
    }
}
